﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using file;
using System.Text.RegularExpressions;
using NPOI.SS.UserModel;
using System.Reflection;
using System.IO;

namespace DMSMT
{
    public partial class HandleFrm : Form
    {
        public HandleFrm()
        {
            InitializeComponent();
        }

        private void btFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "Excel文件|*.xls;*.xlsx";
            ofd.Multiselect = false;
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                tbFile.Text = ofd.FileName;
            }
        }

        private void btDir_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            if (fbd.ShowDialog() == DialogResult.OK)
            {
                tbDir.Text = fbd.SelectedPath;
            }
        }

        private void HandleFrm_Load(object sender, EventArgs e)
        {
            tbDir.Text = @"C:\DMS事件单";
            tbFile.Text = @"C:\DMS上线问题管控表.xlsx";
            tcSRow.Text = "3";
            tcERow.Text = "20";
            tbSheetIndex.Text = "1";
        }

        private void btCreate_Click(object sender, EventArgs e)
        {
            int sheetIndex = Int32.Parse(tbSheetIndex.Text) - 1;
            ExcelReader er = new ExcelReader(1, sheetIndex);
            try
            {
                int sRow = Int32.Parse(tcSRow.Text.Trim());
                int eRow = Int32.Parse(tcERow.Text.Trim());
                er.StratRow = sRow;
                er.EndRow = eRow;
                er.HeadRow = 1;
                string filepath = tbFile.Text.Trim();
                List<List<string>> list = er.read(filepath);
                Log.LogText("读取文件:" + filepath + "\t" + er.SheetName + er.StratRow + "-" + er.EndRow + "行,共" + list.Count + "条数据");
                int[] indexArr = new int[7];
                indexArr[0] = er.HeadList.IndexOf("日期");
                indexArr[1] = er.HeadList.IndexOf("专营店");
                indexArr[2] = er.HeadList.IndexOf("提出人");
                indexArr[3] = er.HeadList.IndexOf("问题描述");
                indexArr[4] = er.HeadList.IndexOf("记录人");
                indexArr[5] = er.HeadList.IndexOf("处理人");
                indexArr[6] = er.HeadList.IndexOf("解决办法/原因");

                for (int i = 0; i < list.Count; i++)
                {
                    List<string> row = list[i];
                    if (row[er.HeadList.IndexOf("状态")].Trim().Equals("验证关闭"))
                    {
                        createHandleExcle(row, indexArr);
                    }
                }
            }
            catch (Exception ex)
            {
                Log.LogError(ex);
            }
        }

        private string GetWritePath(string sIndex, string name)
        {
            string path = tbDir.Text.Trim();
            if (!path.EndsWith("/") && !path.EndsWith("\\"))
            {
                path += "\\";
            }
            path += DateTime.Today.ToString("yyyy-MM-dd") + "\\" + name;
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            path += "\\PVDMS事件处理单" + sIndex + ".xls";
            return path;
        }

        private void createHandleExcle(List<string> list, int[] indexArr)
        {
            string path = Assembly.GetExecutingAssembly().Location;
            path = path.Substring(0, path.LastIndexOf("\\") + 1) + "a.xls";
            IWorkbook wk = ExcelReader.GetWorkBook(path);
            ISheet sheet = wk.GetSheetAt(0);
            IRow row = sheet.GetRow(2);
            string date = list[indexArr[0]];//list[1];
            DateTime d = DateTime.ParseExact(date, "yyyy-MM-dd HH:mm:ss", null);
            if (d.Hour < 8)
            {
                date = d.ToString("yyyy-MM-dd") + " 10:00:00";
            }
            row.GetCell(2).SetCellValue(list[0] + "\r");
            row = sheet.GetRow(6);
            //row.GetCell(2).SetCellValue(list[2]);
            row.GetCell(2).SetCellValue(list[indexArr[1]]);
            row.GetCell(5).SetCellValue(date);
            row = sheet.GetRow(7);
            //row.GetCell(2).SetCellValue(list[3] + "\r");
            row.GetCell(2).SetCellValue(list[indexArr[2]] + "\r");
            row = sheet.GetRow(10);
            //row.GetCell(2).SetCellValue(list[11]);
            row.GetCell(2).SetCellValue(list[indexArr[3]]);
            row = sheet.GetRow(11);
            //row.GetCell(2).SetCellValue(list[4]);
            row.GetCell(2).SetCellValue(list[indexArr[4]]);
            row.GetCell(5).SetCellValue(date);

            string name = list[indexArr[5]];
            row = sheet.GetRow(12);
            //row.GetCell(2).SetCellValue(list[18]);
            row.GetCell(2).SetCellValue(name);
            row.GetCell(5).SetCellValue(date);
            row = sheet.GetRow(15);
            row.GetCell(2).SetCellValue(name); //row.GetCell(2).SetCellValue(list[18]);
            row.GetCell(5).SetCellValue(date);
            string str = list[indexArr[6]];
            row = sheet.GetRow(16);
            row.GetCell(2).SetCellValue(str);
            //row.GetCell(2).SetCellValue(list[20]);
            row = sheet.GetRow(20);
            //row.GetCell(2).SetCellValue(list[20]);
            row.GetCell(2).SetCellValue(str);
            row = sheet.GetRow(21);
            row.GetCell(3).SetCellValue(name); //row.GetCell(3).SetCellValue(list[18]);
            row.GetCell(5).SetCellValue(date);
            row = sheet.GetRow(23);
            row.GetCell(5).SetCellValue(date);

            path = GetWritePath(list[0], name);
            using (FileStream fos = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
            {
                wk.Write(fos);
                Log.LogText("已成功生成文件：" + path);
            }

        }

        private void tb_TextChanged(object sender, EventArgs e)
        {
            TextBox tb = (TextBox)sender;
            Regex reg = new Regex("^[0-9]{1,}$");
            if (!reg.IsMatch(tb.Text.Trim()))
            {
                MessageBox.Show("请输入数字!");
                tb.SelectAll();
                tb.Focus();
            }
        }

        private void tb_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar >= '0' && e.KeyChar <= '9')
            {
                e.Handled = false;
                return;
            }
            e.Handled = true;
        }
    }
}
